// LGUIVSlider.cpp: implementation of the LGUIVSlider class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LGUIVSlider.h"
#include "LGUIContainer.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

LGUIVSlider::LGUIVSlider(const TCHAR *_text, int id, int _x, int _y, int _w, int _h, int _value, int _max, int _min, int _userdata, short16 _knobColor, int _style) :
LGUIWindow(_x,_y,_w,_h)
{
	ID=id; 
	value=_value; 
	minValue=_min; 
	maxValue=_max;
	knobSize=9;
	knobColor=_knobColor;
	dragging=false;
	style=_style;
	text=_text;
	setUserData(_userdata);
}

LGUIVSlider::~LGUIVSlider()
{

}

void LGUIVSlider::penDown(int px, int py)
{
	if (isInside(px,py))
	{
		dragging=true;
		redraw();
	}
}


void LGUIVSlider::penUp(int px, int py)
{
	if (dragging)
		redraw();
	dragging=false;
}

void LGUIVSlider::penMove(int px, int py, bool pressed)
{
	if (!dragging) return;
	py -= (y+knobSize/2);
	if (minValue<maxValue)
		value = (py)*(maxValue-minValue)/(h-knobSize-1);
	else
		value = (h-knobSize-py)*(minValue-maxValue)/(h-knobSize-1);

	clamp();

	if (parent) 
		parent->deliverMessage(LGUIMessage(MSG_SLIDERCHANGED,ID,value,userData));

	redraw();
}

void LGUIVSlider::draw(int xoff, int yoff)
{
	if (isDirty)
	{
		//buffer.rectFill(x+xoff+1,y+yoff+1,x+xoff+w,y+yoff+h,COLOR(0x404040));
		for (int i=1; i<w; i++)
		{
			buffer.vLine(x+xoff+i,y+yoff+1,y+yoff+h,((i>>1)&1) ? COLOR(0x403838) : COLOR(0x201818));
		}
		buffer.rect(x+xoff,y+yoff,x+xoff+w,y+yoff+h,dragging ? COLOR(0x80ff80) : COLOR(0x404040));
		int knobY = y+yoff+(h-knobSize)*(value-minValue)/(maxValue-minValue);
		buffer.rectFillDarkFaded(x+xoff+1,knobY,x+xoff+w,knobY+knobSize,dragging ? whitenColor(knobColor): knobColor);
		buffer.rectFillDarkFaded(x+xoff+1,knobY,x+xoff+w,knobY+knobSize,dragging ? whitenColor(knobColor): knobColor);

		buffer.hLine(x+xoff+1,knobY+2,x+xoff+w,dragging ? whitenColor(whitenColor(knobColor)): whitenColor(knobColor));
		if (style == 1)
			buffer.rectFillDarkFaded(x+xoff+1,knobY+knobSize,x+xoff+w,y+yoff+h,dragging ? (knobColor): darkenColor(knobColor));

		buffer.rect(x + xoff, knobY, x + xoff + w, knobY + knobSize, dragging ? COLOR(0x80ff80): COLOR(0x0));
		if (text)
			buffer.drawTextCenter(text, x+xoff+w/2-1,y+yoff+h+7, 0x0, 0);

		TCHAR temp[8];
		_stprintf(temp,TEXT("%i"),value);
			buffer.drawTextShadowCenter(temp,x+xoff+w/2-1,knobY+5,0xffff);

		LRect dirty = *this;
		dirty.x+=xoff;
		dirty.y+=yoff;
		dirty.w++;
		dirty.h+=16;
		buffer.addDirtyRect(dirty);
		isDirty = false;
	}
}

void LGUIVSlider::clamp()
{
	if (minValue<maxValue)
	{
		if (value<minValue) value=minValue;
		if (value>maxValue) value=maxValue;
	}
	else
	{
		if (value>minValue) value=minValue;
		if (value<maxValue) value=maxValue;
	}
}
